//
//  XMLParser.m
//  Exchange
//
//  Created by Александр Лазарекно on 09.12.12.
//  Copyright (c) 2012 Бонч. All rights reserved.
//

#import "XMLParser.h"
#import "Folder.h"
#import "DBAccesor.h"

@implementation XMLParser
+ (void) syncFolderHierarchyParse:(GDataXMLDocument*) data
{
    NSDictionary *namespaces = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"http://schemas.microsoft.com/exchange/services/2006/messages", @"m",
                                @"http://schemas.microsoft.com/exchange/services/2006/types", @"t",
                                @"http://www.w3.org/2001/XMLSchema-instance", @"xsi",
                                @"http://www.w3.org/2001/XMLSchema", @"xsd",
                                @"http://schemas.xmlsoap.org/soap/envelope/", @"s", nil];
    NSArray* foldersXMl = [data nodesForXPath:@"//t:Create/t:Folder" namespaces:namespaces error:nil];
    Folder* folder = [[Folder alloc]init];
    for (GDataXMLElement *folders in foldersXMl){
    NSArray* namesXML = [folders elementsForName:@"t:DisplayName"];
    NSArray* folderIDXML = [folders elementsForName:@"t:FolderId"];
    NSArray* parentFolderIDXML = [folders elementsForName:@"t:ParentFolderId"]; 
        //NSLog(@"%@",[folderID objectAtIndex:0]);
        
        if (namesXML.count > 0) {
            GDataXMLElement *firstName = (GDataXMLElement *) [namesXML objectAtIndex:0];
            folder.displayName = firstName.stringValue;
            NSLog(@"%@",folder.displayName);
            folder.folderID = [[(GDataXMLElement *)[folderIDXML objectAtIndex:0] attributeForName:@"Id"]stringValue];
            folder.parentFolderID = [[(GDataXMLElement *)[parentFolderIDXML objectAtIndex:0] attributeForName:@"Id"]stringValue];
            [DBAccesor insertFolders:folder];
            
        }
         
    }
    [folder release];

}

@end
